<template>
    <div>
        <div class="layout-route" v-show="!editData.visible">
            <div class="card card-search">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                    <el-form-item label="产品类型" prop="product_type">
                        <el-select v-model="queryParams.product_type" placeholder="请选择产品类型" clearable style="width: 240px">
                            <el-option v-for="(dict, index) in dict.productTypeList" :key="index"  :label="dict.type_name" :value="dict.type_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="SPU" prop="spu">
                        <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="产品变体" prop="product_variant_type">
                        <el-select v-model="queryParams.product_variant_type" placeholder="全部" clearable style="width: 240px">
                            <el-option v-for="(dict, index) in dict.productVariantTypeList" :key="index" :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="产品标题" prop="product_title">
                        <el-input v-model.trim="queryParams.product_title" placeholder="请输入产品系列" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="创建时间" prop="create_time">
                        <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label=" ">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <div class="card">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" @click="gotoDetail">创建产品</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
                <el-table v-loading="loading" :data="tableData" row-key="id" border script>
                    <el-table-column label="产品主图" min-width="100">
                        <template slot-scope="scope">
                            <ImageUploadImg v-model="scope.row.basic_image" :readOnly="true"></ImageUploadImg>
                        </template>
                    </el-table-column>
                    <el-table-column label="SPU" prop="spu" min-width="60">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="showSpuListDialog(scope.row)">{{ scope.row.spu }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="产品标题" prop="product_title" min-width="300">
                        <template slot-scope="scope">
                            <div>{{ scope.row.product_title }}</div>
                            <div style="color: #999">{{ scope.row.shop_name }}</div>
                            <el-popover placement="top" trigger="hover">
                                <span slot="reference">{{ scope.row.product_id_api }}</span>
                                <el-button @click="copyText(scope.row.product_id_api)">复制</el-button>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column label="分类" prop="product_type_value" min-width="120" show-overflow-tooltip></el-table-column>
                    <el-table-column label="变体类型" prop="product_variant_type_value" min-width="120" show-overflow-tooltip></el-table-column>
                    <!-- <el-table-column label="SKU信息" prop="sku_list" min-width="260" class-name="border-cell">
                        <template slot-scope="scope" v-if="scope.row.sku_list && scope.row.sku_list.length > 0">
                            <div v-for="(item, index) in scope.row.sku_list" :key="index" class="border-cell-item">
                                <el-popover placement="top" trigger="hover">
                                    <span slot="reference">{{ item.sku }}</span>
                                    <el-button @click="copyText(item.sku)">复制</el-button>
                                </el-popover>
                            </div>
                        </template>
                    </el-table-column> -->

                    <!-- <el-table-column label="时间" prop="purchase_cost" min-width="180">
                        <template slot-scope="scope">
                            <div><span style="color: #999">创建：</span>{{ scope.row.create_time }}</div>
                            <div><span style="color: #999">更新：</span>{{ scope.row.update_time }}</div>
                            <div><span style="color: #999">平台刊登：</span>{{ scope.row.listing_time }}</div>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="创建时间：" prop="spu" min-width="120">
                        <template slot-scope="scope">
                            {{ scope.row.create_time}}
                        </template>
                    </el-table-column>
                    <el-table-column label="更新时间：" prop="spu" min-width="120">
                        <template slot-scope="scope">
                            {{ scope.row.update_time}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" fixed="right">
                        <template slot-scope="scope">
                            <div><el-button type="text" icon="el-icon-edit" @click="gotoDetail(scope.row)">编辑</el-button></div>
                            <div><el-button type="text" icon="el-icon-upload2" @click="showSyncShopDialog(scope.row)">导入</el-button></div>
                            <div><el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button></div>
                            <!-- <el-popover
                                    placement="top"
                                    trigger="hover">
                                    <el-button slot="reference" type="text">更多<i class="el-icon-arrow-down"></i></el-button>
                                    <div>
                                        <div><el-button type="text">复制为“新产品”</el-button></div>
                                        <div><el-button type="text">下架产品</el-button></div>
                                        <div><el-button type="text">添加备注</el-button></div>
                                        <div><el-button type="text" @click="deleteItemP(scope.row)">删除</el-button></div>
                                    </div>
                                </el-popover> -->
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
            </div>
            <shopDialog v-model="syncDialog.visible" :params="syncDialog.data" @success="handleQuery"></shopDialog>

            <SpuListDialog v-model="spuListDialog.visible" v-if="spuListDialog.visible" :params="spuListDialog.data"></SpuListDialog>

        </div>
        <product-detail v-show="editData.visible" :params="editData.data" @success="handleQuery" @cancel="editData.visible=false"></product-detail>
    </div>
</template>
<script>
import { getAttrList } from '@/api/product'
import { queryListingList,apiproductDelete } from '@/api/listing'
import { parseTime } from '@/utils/quickadmin'
import { PRODUCT_VARIANT_TYPE } from '../constant'
import shopDialog from './components/shopDialog.vue'
import SpuListDialog from './components/spuListDialog.vue'
import ProductDetail from './detail'
export default {
    name: 'ProductOnLineList',
    components: { shopDialog,SpuListDialog, ProductDetail },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],

            // 状态数据字典
            dict: {
                productVariantTypeList: PRODUCT_VARIANT_TYPE,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                product_type: undefined,
                spu: undefined,
                product_variant_type: undefined,
                product_title: undefined,
                create_time: [],
            },
            syncDialog: {
                visible: false,
                data: {}
            },
            
            spuListDialog: {
                visible: false,
                data: {},
            },
            editData: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const productTypeData = await getAttrList()
            this.dict.productTypeList = productTypeData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.getList()
    },
    methods: {
        expendItem(expend) {
            expend = expend == 1 ? 2 : 1
        },
        gotoDetail(row) {
            this.editData.visible = true
            this.editData.data = row
            // this.$router.push({
            //     path: '/Listing/Public/productOnline/detail',
            //     query: { id: row.id, model: 'public' },
            // })
        },
        showSyncShopDialog(row) {
            this.syncDialog.visible = true
            this.syncDialog.data.product_ids = [row.id]
        },
        copyText(string) {
            // navigator.clipboard.writeText 该方法需要在安全域下才能够使用，比如：https 协议的地址、127.0.0.1、localhost
            navigator.clipboard
                .writeText(string)
                .then(() => {
                    this.$message.success('复制成功')
                })
                .catch(err => {
                    // 复制失败
                    console.info('复制失败')
                })
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            queryListingList(qyparams)
                .then(response => {
                    this.tableData = response.data.list?.map(item => {
                        item.expend = item.sku_list?.length > 3 ? 2 : null
                        return item
                    })
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 搜索按钮操作
        handleQuery2() {
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        
        showSpuListDialog(row) {
            this.spuListDialog.visible = true
            this.spuListDialog.data = row
        },
        // 删除按钮操作
        handleDelete(row) {
            let tableIds = row.id || this.ids
            tableIds = tableIds.toString()
            this.$confirm('是否确认删除?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(async () => {
                    await apiproductDelete({ id: tableIds })
                    this.getList()
                    this.msgSuccess('删除成功')
                })
                .catch(() => {})
        },

    },
}
</script>
<style scoped>
.custom-select .el-select-dropdown__item {
    max-width: 450px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.border-cell {
    padding: 0 !important;
}
.border-cell .cell {
    padding: 0 !important;
}
.border-cell-item {
    padding: 10px;
    border-bottom: 1px solid #dfe6ec;
}
.border-cell-item:last-child {
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
